package com.su.sweet.controller;


import com.su.sweet.model.SRole;
import com.su.sweet.service.ISRoleService;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Result;

import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author su
 * @since 2025-01-02
 */
@Controller
@Mapping("/role")
public class SRoleController {
    @Inject
    private ISRoleService roleService;

    @Post
    @Mapping("/save")
    public Result save(SRole role, Context ctx) {
        role.setCreateTime(LocalDateTime.now());
        role.setCreateUser(Long.valueOf(ctx.header("userId")));
        role.setDelFlag("N");
        role.setDataScopeType(50);
        role.setVersionFlag(System.currentTimeMillis());
        return Result.succeed(roleService.insert(role));
    }

    @Post
    @Mapping("/update")
    public Result update(SRole role, Context ctx) {
        role.setUpdateTime(LocalDateTime.now());
        role.setUpdateUser(Long.valueOf(ctx.header("userId")));
        return Result.succeed(roleService.update(role));
    }

    @Post
    @Mapping("/delete")
    public Result delete(Integer id) {
        return Result.succeed(roleService.delete(id));
    }

    @Get
    @Mapping("/list")
    public Result list(Integer current, Integer pageSize) {
        return Result.succeed(roleService.list(current, pageSize));
    }

    @Get
    @Mapping("/all")
    public Result all() {
        return Result.succeed(roleService.allRole());
    }
}
